package cn.caipengbo.file;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;

/**
 * Created by Myth on 2/2/2017.
 */
public class UploadFileAction extends ActionSupport {
    private File photo; //名字要对应表单
    private String photoFileName; // 文件名字，XXXFileName 固定写法
    private String photoContentType; // MIME类型，XXXContentType也是固定写法

    public String upload() throws IOException {
        ServletContext servletContext =  ServletActionContext.getServletContext();
        String path = servletContext.getRealPath("/files");
        //构建目标文件
        System.out.println(path);
        if (photoFileName==null)
            System.out.println("photoFileName is Null");
        File target = new File(path,photoFileName);
        System.out.println(path);
        //使用FileUtils保存
        FileUtils.copyFile(photo,target);
        return SUCCESS;
    }

    public File getPhoto() {
        return photo;
    }

    public void setPhoto(File photo) {
        this.photo = photo;
    }

    public String getPhotoFileName() {
        return photoFileName;
    }

    public void setPhotoFileName(String photoFileName) {
        this.photoFileName = photoFileName;
    }

    public String getPhotoContentType() {
        return photoContentType;
    }

    public void setPhotoContentType(String photoContentType) {
        this.photoContentType = photoContentType;
    }
}
